﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using FinPlusCommon;
using log = Logger.Logger;

namespace FinPlusWPFControls
{
    //Note Prime use edit template columns use primative controls for data template
    public partial class FinPlusValueComboBox : FinPlusControl
    {
        public override string Id { get { return GetValue(IdProperty).ToString(); } set { } }
        public override string[] TestStates { get { return new string[] { "TestString", "ViewString", "IsValid" }; } }
        public override string TestString { get { return string.Format("{0}|{1}", IsEnabled, Text); } }

        public string ViewString { get { return string.Format("{0}|{1}", comboBox.IsEnabled, comboBox.SelectedValue); } }
        public string FunctionName { get { return GetValue(FunctionNameProperty).ToString(); } set { } }
        public object Text { get { return GetValue(TextProperty); } set { SetValue(TextProperty, value); } }
        public new bool IsEnabled { get { return (bool)GetValue(IsEnabledProperty); } set { } }
        public Dictionary<string, bool> ItemSource { get { return (Dictionary<string, bool>)GetValue(ItemSourceProperty); } set { } }

        public static readonly DependencyProperty IdProperty = DependencyProperty.Register(Propertys.Id.ToString(), typeof(string), typeof(FinPlusValueComboBox));
        public static readonly DependencyProperty FunctionNameProperty = DependencyProperty.Register(Propertys.FunctionName.ToString(), typeof(string), typeof(FinPlusValueComboBox));
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register(Propertys.Text.ToString(), typeof(object), typeof(FinPlusValueComboBox), new UIPropertyMetadata(Callback));
        public new static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register(Propertys.IsEnabled.ToString(), typeof(bool), typeof(FinPlusValueComboBox));
        public static readonly DependencyProperty ItemSourceProperty = DependencyProperty.Register(Propertys.ItemSource.ToString(), typeof(Dictionary<string, bool>), typeof(FinPlusValueComboBox));

        private enum Propertys { Id, FunctionName, Text, IsEnabled, ItemSource }
        private bool _hasSetEnable;

        //construct
        public FinPlusValueComboBox()
        {
            InitializeComponent();
            IsValid = true;
        }

        //common control interface
        public override void CmdExecute(IFunction command) 
        {
            try
            {
                CmdCapture(command);

                switch ((Cmds)Enum.Parse(typeof(Cmds), command.Name, true))
                {
                    case Cmds.DropDownOpened: DropDownOpened(); break;
                    case Cmds.Checked: Checked(command["Tag"].ToString()); break;
                    case Cmds.Unchecked: Unchecked(command["Tag"].ToString()); break;
                    default: throw new Exception(string.Format("event not recognised {0}", command.Name));
                }
            }
            catch (CmdExecuteException e)
            {
                log.Log(Logger.Level.Error, Id, Config, e.Message, e);
            }
        }

        //private
        private void SetControl()
        {
            //permission
            if (!_hasSetEnable)
            {
                comboBox.IsEnabled = IsEnabled &&  GetPermission(FunctionName, Propertys.Id.ToString(), Id).CanWrite;
                _hasSetEnable = true;
            }
        }

        private bool DropDownOpened()
        {
            foreach (var val in Text.ToString().Split(','))
                if (ItemSource.ContainsKey(val))
                    ItemSource[val] = true;
            
            comboBox.ItemsSource = ItemSource;
            return true;
        }

        private bool Checked(string content)
        {
            var values = Text.ToString().Split(',').ToList<string>();
            if (values.Contains(content)) 
                return false;
        
            values.Add(content);
            values.Remove(string.Empty);

            Text = string.Join(",", values);
            return true;
        }

        private bool Unchecked(string content)
        {
            var values = Text.ToString().Split(',').ToList<string>();
            if (!values.Contains(content)) return false;

            values.Remove(content);
            values.Remove(string.Empty);

            Text = string.Join(",", values);
            return true;
        }

        //cmds
        private enum Cmds { DropDownOpened, Checked, Unchecked }

        private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs a) { ((FinPlusValueComboBox)d).SetControl(); }
        private void DropDown_Opened(object s, EventArgs a) { CmdRouter(Cmds.DropDownOpened.ToString()); }
        private void Checked(object s, RoutedEventArgs a) { CmdRouter(Cmds.Checked.ToString(), ((CheckBox)s).Content != null ? ((CheckBox)s).Content.ToString() : string.Empty); }
        private void Unchecked(object s, RoutedEventArgs a) { CmdRouter(Cmds.Unchecked.ToString(), ((CheckBox)s).Content != null ? ((CheckBox)s).Content.ToString() : string.Empty); }
    }
}
